﻿using System;
using System.Windows.Input;
using AIPASoft.Interfaces.Base;
using AIPASoft.Interfaces.BusinessDomain;
using AIPASoft.Interfaces.Security;

namespace AIPASoft.SecurityLibrary.ViewModel
{
    public class LoginWindowViewModel : ViewModelBase,IDisposable
    { 

        public LoginWindowViewModel()
        { 
        }

        private string _loginMessage;
        public string LoginMessage
        {
            get
            {
                return _loginMessage;
            }
            set
            {
                _loginMessage = value;
                NotifyPropertyChanged("LoginMessage");
            }
        }

        private string _userName;
        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                _userName = value;
                NotifyPropertyChanged("UserName");
            }
        }

        private string _password;
        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                _password = value;
                NotifyPropertyChanged("Password");
            }
        }

        private IUserRealm _userRealm;
        public IUserRealm UserRelam
        {

            get { return _userRealm; }
            set
            {
                _userRealm = value;
                NotifyPropertyChanged("UserRealm");
            }
        }
        
        private ICommand _loginCommand; 
        public ICommand LoginCommand
        {
            get
            {
                return _loginCommand ?? (_loginCommand = new RelayCommand(param => this.Login(),
                                                                          null));
            }
        }

        private ICommand _closeCommand; 
        public ICommand CloseCommand
        {
            get
            {
                return _closeCommand ?? (_closeCommand = new RelayCommand(param => this.Close(),
                                                                          null));
            }
        }

        public delegate void ViewCloseEventHandler(object source, EventArgs e);

        public ViewCloseEventHandler OnWindowClose;

       

        private void Login()
        {
            IUserRealm userRealm;
            var sdp = new DataProvider.SecurityDataProvider();
            try
            {
                userRealm = sdp.Login(UserName, Password);
                
                if(userRealm!=null)
                    if (userRealm.User != null)
                    {
                        userRealm.LoginTime = DateTime.Now;
                        UserRelam = userRealm;
                    }
                Close();

            }
            catch (Exception ex)
            {
                LoginMessage = ex.Message;
            }
        }

        private void Close()
        {
            if (OnWindowClose != null)
            {
                Dispose();
                OnWindowClose(this, new EventArgs());
            }
        }


        public void Dispose()
        {
            this.LoginMessage = "";
            this.UserName = "";
            this.Password = "";
            this.UserRelam = null;
        }
    }
}
